package com.codegen.controller;

import jakarta.annotation.Resource;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.codegen.generator.CodeGenerator;

@RestController
@RequestMapping("/api/codegen")
public class CodeGenController {

    @Resource
    private CodeGenerator codeGenerator;

    @PostMapping("/generate")
    public Map<String, Object> generate(@RequestBody CodeGenRequest request) {
        Map<String, Object> result = new HashMap<>();
        try {
            codeGenerator.generate(
                request.getUrl(),
                request.getUsername(),
                request.getPassword(),
                request.getPackageName(),
                request.getAuthor(),
                request.getOutputPath(),
                request.getTables()
            );
            result.put("success", true);
            result.put("message", "代码生成成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "代码生成失败: " + e.getMessage());
        }
        return result;
    }

    @Data
    public static class CodeGenRequest {
        private List<String> tables;
        private String author;
        private String packageName;
        private String outputPath;
        private String url;
        private String username;
        private String password;
    }
} 